﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using InputBoxSample;
using NHibernate.Criterion;
using PatentApp.Models;
using PatentApp.Data;
using iTextSharp.text;
using iTextSharp.text.pdf;
using Font = iTextSharp.text.Font;

namespace PatentApp
{
    public partial class dlgRaport : Form
    {
        private Access access;

        public dlgRaport()
        {
        }

        public dlgRaport(Access access)
        {
            InitializeComponent();
            this.access = access;
        }

        private void btnAddCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
        }

        private void btnAddNext_Click(object sender, EventArgs e)
        {
            result = new List<Produkt>();
            switch (ddlFindCategory.SelectedIndex)
            {
                case 0:
                    using (var session = NHibernateCfg.OpenSession())
                        result.AddRange((from prods in session.QueryOver<Produkt>() orderby prods.DataOgloPat select prods).Asc.List<Produkt>());
                    break;
                case 1:
                    using (var session = NHibernateCfg.OpenSession()){
                        List<Pracownik> pracList = (from prac in session.QueryOver<Pracownik>() where (prac.Imie.IsLike("%" + txtFindValue.Text + "%") || prac.Nazwisko.IsLike("%" + txtFindValue.Text + "%")) select prac).List<Pracownik>().ToList<Pracownik>();
                        if(pracList.Count>0)
                            foreach (Pracownik item in pracList)
                               result.AddRange(item.Produkty);
                    }
                    break;
                case 2:
                    using (var session = NHibernateCfg.OpenSession()){
                        Kategoria cat = (from prac in session.QueryOver<Kategoria>() where prac.Nazwa.IsLike("%" + txtFindValue.Text + "%") select prac).SingleOrDefault();
                        result.AddRange((from prods in session.QueryOver<Produkt>() where prods.Kategoria.KategoriaId == cat.KategoriaId orderby prods.DataOgloPat select prods).Asc.List<Produkt>());
                    }
                    break;
                case 3:
                    using (var session = NHibernateCfg.OpenSession())
                        result.AddRange((from prods in session.QueryOver<Produkt>() where prods.SlowoKluczowe.IsLike("%" + txtFindValue.Text + "%") orderby prods.DataOgloPat select prods).Asc.List<Produkt>());
                    break;  
                case 4:
                    using (var session = NHibernateCfg.OpenSession())
                        result.AddRange((from prods in session.QueryOver<Produkt>() where prods.WlascicielPraw.IsLike("%" + txtFindValue.Text + "%") orderby prods.DataOgloPat select prods).Asc.List<Produkt>());
                    break;
                case 5:
                    using (var session = NHibernateCfg.OpenSession())
                        result.AddRange((from prods in session.QueryOver<Produkt>() orderby prods.DataZglTworcy select prods).Asc.List<Produkt>().ToList<Produkt>().FindAll(x => x.DataZglTworcy.Year == int.Parse(txtFindValue.Text)));
                    break;
                case 6:
                    using (var session = NHibernateCfg.OpenSession())
                    {
                        List<Pracownik> pracList = (from prac in session.QueryOver<Pracownik>() where (prac.Wydzial.IsLike("%" + txtFindValue.Text + "%")) select prac).List<Pracownik>().ToList<Pracownik>();
                        if(pracList.Count>0)
                            foreach (Pracownik item in pracList)
                                result.AddRange(item.Produkty);
                    }
                    break;
                default:
                    break;
            }
            if (result == null || result.Count==0)
                MessageBox.Show("Nie znalezionego zgłoszenia o podobnym kryterium");
            else
                Print();
        }

        private PdfPTable _stateTable(Produkt produkt)
        {
            PdfPTable table = new PdfPTable(4);
            table.DefaultCell.Border = 0;
            table.WidthPercentage = 100;
            table.SetWidths(new Single[] {2, 3, 2, 3});
            Font defFont = FontFactory.GetFont(FontFactory.HELVETICA, "CP1250", 14,1,BaseColor.BLACK);

            table.SpacingBefore = 10;

            table.AddCell(new Phrase("Numer identyfikacyjny:",defFont));
            table.AddCell(new Phrase(produkt.NrOgolny + " / " + produkt.NrRoczny + " / " + produkt.DataZglTworcy.ToShortDateString(),defFont));
            table.AddCell("");
            table.AddCell("");

            table.AddCell(new Phrase("Tytuł zgłoszenia:",defFont));
            table.AddCell(new Phrase(produkt.Tytul,defFont));
            table.AddCell("");
            table.AddCell("");

            table.AddCell(new Phrase("Numer zgłoszenia:",defFont));
            table.AddCell(produkt.NrZgloszenia);
            table.AddCell(new Phrase("Numer patentu:",defFont));
            table.AddCell(produkt.NrPatentu);

            table.AddCell(new Phrase("Data zgłoszenia:",defFont));
            table.AddCell(new Phrase((produkt.DataZgloszenia.HasValue)?produkt.DataZgloszenia.Value.ToShortDateString():"Brak",defFont));
            table.AddCell(new Phrase("Data ogłoszenia patentu:",defFont));
            table.AddCell(new Phrase((produkt.DataOgloPat.HasValue) ? produkt.DataOgloPat.Value.ToShortDateString() : "Brak",defFont));

            table.AddCell(new Phrase("Właściciel praw:",defFont));
            table.AddCell(new Phrase(produkt.WlascicielPraw,defFont));
            table.AddCell("");
            table.AddCell("");

            table.AddCell(new Phrase("Kategoria:",defFont));
            using (var session = NHibernateCfg.OpenSession())
                table.AddCell(new Phrase(produkt.Kategoria.Nazwa,defFont));
            table.AddCell("");
            table.AddCell("");
            table.AddCell(new Phrase("Współtwórcy:", defFont));
            String twurcy = "";
            foreach (Pracownik p in produkt.Pracownicy)
                twurcy += p.Imie + " " + p.Nazwisko + " , ";
            PdfPCell cell = new PdfPCell();
            cell.Colspan = 3;
            cell.Border = 0;
            cell.Phrase = new Phrase(twurcy, defFont);
            table.AddCell(cell);

            PdfPCell hr = new PdfPCell();
            hr.Colspan = 4;
            hr.Border = 1;
            table.AddCell(hr);

            return table;
        }  
        private void Print()
        {
            if(File.Exists("Raport.pdf"))
                File.Delete("Raport.pdf");
            Document myDocument = new Document();
            try{
                PdfWriter.GetInstance(myDocument, new FileStream("Raport.pdf", FileMode.Create));
                myDocument.Open();
                Font defFont = FontFactory.GetFont(FontFactory.HELVETICA, "CP1250", 14,1, BaseColor.BLACK);
                PdfPTable table = new PdfPTable(4);
                table.DefaultCell.Border = 0;
                table.WidthPercentage = 100;
                table.SetWidths(new Single[] { 2, 3, 2, 3 });
                table.SpacingBefore = 10;
                PdfPCell cell = new PdfPCell();
                cell.Colspan = 4;
                cell.Border = 0;
                cell.Phrase = new Phrase(ddlFindCategory.SelectedItem.ToString(), defFont);
                table.AddCell(cell);
                table.AddCell(new Phrase("Data raportu:",defFont));
                table.AddCell(new Phrase(DateTime.Now.ToShortDateString(), defFont));
                table.AddCell(new Phrase("Dodający:",defFont));
                table.AddCell(new Phrase((this.access==null)?"Brak":access.Login,defFont));
                PdfPCell hr = new PdfPCell();
                hr.Colspan = 4;
                hr.Border = 1;
                table.AddCell(hr);
                myDocument.Add(table);

                foreach (Produkt produkt in result)
                {
                    myDocument.Add(_stateTable(produkt));
                }
            }
            catch (DocumentException de)
            {
                Console.Error.WriteLine(de.Message);
            }
            catch (IOException ioe)
            {
                Console.Error.WriteLine(ioe.Message);
            }
            myDocument.Close();
            Process.Start("Raport.pdf");
        }

         public List<Produkt> result {get; set; }

        private void ddlFindCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<String> strList = new List<String>();
            dlgValues dlg;
            switch (ddlFindCategory.SelectedIndex)
            {
                case 1:
                    using (var session = NHibernateCfg.OpenSession())
                        strList.AddRange((from prac in session.QueryOver<Pracownik>() where prac.PracownikId != null select prac.Imie).List<String>());
                    dlg = new dlgValues(strList);
                    if (dlg.ShowDialog() == DialogResult.OK)
                        txtFindValue.Text = dlg.result;
                    break;
                case 2:
                    using (var session = NHibernateCfg.OpenSession())
                        strList.AddRange((from cat in session.QueryOver<Kategoria>() where cat.KategoriaId != null select cat.Nazwa).List<String>());
                    dlg = new dlgValues(strList);
                    if (dlg.ShowDialog() == DialogResult.OK)
                        txtFindValue.Text = dlg.result;
                    break;
                case 3:
                    using (var session = NHibernateCfg.OpenSession())
                        strList.AddRange((from prods in session.QueryOver<Produkt>() where prods.ProduktId != null select prods.SlowoKluczowe).List<string>().ToList<string>());
                    dlg = new dlgValues(strList);
                    if (dlg.ShowDialog() == DialogResult.OK)
                        txtFindValue.Text = dlg.result;
                    break;  
                case 4:
                    using (var session = NHibernateCfg.OpenSession())
                        strList.AddRange((from prods in session.QueryOver<Produkt>() select prods.WlascicielPraw).List<string>().ToList<string>());
                    dlg = new dlgValues(strList);
                    if (dlg.ShowDialog() == DialogResult.OK)
                        txtFindValue.Text = dlg.result;
                    break;
                case 5:
                    for (int i = DateTime.Now.Year - 10; i < DateTime.Now.Year + 10; i++)
                        strList.Add(i.ToString());
                    dlg = new dlgValues(strList);
                    if (dlg.ShowDialog() == DialogResult.OK)
                        txtFindValue.Text = dlg.result;
                    break;
                case 6:
                    using (var session = NHibernateCfg.OpenSession())
                    {
                        List<Pracownik> pracList = (from prac in session.QueryOver<Pracownik>() where prac.PracownikId != null select prac).List<Pracownik>().ToList<Pracownik>();
                        if(pracList.Count>0)
                            foreach (Pracownik item in pracList.Distinct())
                                strList.Add(item.Wydzial);
                    }
                    dlg = new dlgValues(strList);
                    if (dlg.ShowDialog() == DialogResult.OK)
                        txtFindValue.Text = dlg.result;
                    break;
                default:
                    break;
            }


        }
    }
}
